//
//  ViewController.m
//  Contact
//
//  Created by LeeJay on 2017/3/20.
//  Copyright © 2017年 LeeJay. All rights reserved.
//

#import "ViewController.h"
#import <ContactsUI/ContactsUI.h>
#import <Contacts/Contacts.h>

@interface ViewController () <CNContactPickerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self getAuthor];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    [self picker];
    
    [self contacts];
}

- (void)picker
{
    CNContactPickerViewController *cpvc = [[CNContactPickerViewController alloc] init];
    cpvc.delegate = self;
    [self presentViewController:cpvc animated:YES completion:nil];
}

- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
    NSLog(@"取消");
}

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    NSLog(@"%@---%@", contact.namePrefix, contact.familyName);
    NSArray<CNLabeledValue<CNPhoneNumber*>*>  *phoneNumbers = contact.phoneNumbers;
    [phoneNumbers enumerateObjectsUsingBlock:^(CNLabeledValue<CNPhoneNumber *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@---%@", obj.label, obj.value.stringValue);
    }];
}

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
    NSLog(@"%@---%@", contactProperty.key, contactProperty.value);
}

- (void)contacts
{
    CNContactStore *contactStore = [CNContactStore new];
    NSArray *keys = @[CNContactFamilyNameKey,
                      CNContactGivenNameKey,
                      ];
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
    [contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
        
        // 获取姓名
        NSString *familyName = contact.familyName;
        NSString *lastName = contact.givenName;
        NSLog(@"%@---%@", familyName, lastName);
        
        if ([contact isKeyAvailable:CNContactPhoneNumbersKey]) {
            for (CNLabeledValue *labeledValue in contact.phoneNumbers)
            {
                CNPhoneNumber *phoneValue = labeledValue.value;
                NSString *phoneNumber = phoneValue.stringValue;
                
                NSLog(@"%@--%@",phoneNumber, labeledValue.label);
            }
        }

        
       
    }];
}

- (void)getAuthor
{
    // 判断当前的授权状态是否是用户还未选择的状态
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusNotDetermined) {
    
        CNContactStore *store = [CNContactStore new];
        [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
            
            if (granted)
            {
                NSLog(@"授权成功!");
            }
            else
            {
                NSLog(@"授权失败!");
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
